<?php

namespace app\api\logic;

use think\facade\Db;
use app\data\util\HttpUtil;
use think\exception\ValidateException;
use think\facade\Cache;

class ApiAddTagLogic
{
    public function __construct()
    {

    }

    public function ApiProductList($params = [])
    {
        
    }

    public function ApiCustomerAddTag($params = [])
    {
        $shop_id = $params['shop_id'] ?? 17;
        $customer = Db::name('order_customer')
            ->where('shop_id',$shop_id)
            ->where('addtags',0)
            // ->where('customer_id',9211142963570)
            ->order("id desc")
            ->find();
        if(empty($customer)){
            return json(DefaultResponse(201, false, "暂无数据", null)); 
        }
        $shop_info = Db::name('shops')->where('id',$shop_id)->find();
        $url = $shop_info['myshopify_domain'] . "/admin/api/2023-07/customers/".$customer['customer_id'].".json";
        $return = HttpUtil::getUrl($url, $shop_info);
        if (!empty($return['errors'])) {
            $update['addtags'] = 2;
            Db::name('order_customer')
                ->where('customer_id',$customer['customer_id'])
                ->update($update);
            return json(DefaultResponse(201, false, json_encode($return['errors']), null));
        }
        $customers = $return['customer'];
        if (!empty($customers)) {
            $customer_tags = $customers['tags'] ?? ""; //原始标签
            $spus = Db::name('order_achievement')
                ->field('spu')
                ->where('order_id',$customer['order_id'])
                ->group('spu')
                ->select()->toarray();
                //保留三个标签
                $spu_tags = array_slice($spus,0,3);
                foreach ($spu_tags as $key => $value) {
                    $spu_tags2[] = $value['spu'];
                }
                //修改成字符串
                $spu_tags = implode(",",$spu_tags2);
                if(!empty($customer_tags)){
                    $tags_new = $customer_tags.",".$spu_tags;
                }else{
                    $tags_new = $spu_tags;
                }
            $tags_new = array_unique(explode(",", $tags_new));
            $post['customer']['id'] = $customer['customer_id'];
            $post['customer']['tags'] = $tags_new;
            $url = $shop_info['myshopify_domain'] . "/admin/api/2023-07/customers/".$customer['customer_id'].".json";
            $return = HttpUtil::putUrl($url,$post,$shop_info);
            if (!empty($return['errors'])) {
                $update['addtags'] = 2;
                Db::name('order_customer')
                    ->where('customer_id',$customer['customer_id'])
                    ->update($update);
                return json(DefaultResponse(201, false, json_encode($return['errors']), null));
            }
        }
        $update['addtags'] = 1;
        $return = Db::name('order_customer')
            ->where('customer_id',$customer['customer_id'])
            ->update($update);
        return json(DefaultResponse(200, false, "完成", $customer['customer_id'])); 
    }
    public function ApiProductAddTag($params = [])
    {
        $order = Db::name('order_achievement')
            ->field('order_id,shop_id,spu')
            ->where('id',"=","593407")
            // ->where('spu',"<>","")
            // ->where('shop_id',17)
            // ->where('tag_status',0)
            ->order('id desc')
            ->find();
        if(empty($order)){
            return json(DefaultResponse(201, false, "暂无数据", null)); 
        }
        $shop_info      = Db::name('shops')->where('id',$order['shop_id'])->find();
        $url = $shop_info['myshopify_domain'] . "/admin/api/2023-07/orders/".$order['order_id'].".json";
        // halt($url);
        $return = HttpUtil::getUrl($url, $shop_info);
        if (!empty($return['errors'])) {
            $update['tag_status'] = 2;
            Db::name('order_achievement')
                ->where('order_id',$order['order_id'])
                ->update($update);
            return json(DefaultResponse(201, false, json_encode($return['errors']), null));
        }
        halt($return);
        $orders = $return['order'];
        if (!empty($orders)) {
            $order_tags = $orders['tags'] ?? ""; //原始标签
            $spu_tags = 
            $post['order']['id'] = $order['id'];
            $post['order']['tags'] = $tags;
            $url = $shop_info['myshopify_domain'].$version . $operation."/" . $order['id'] . ".json";
            $return = HttpUtil::putUrl($url, $shop_info);
        }
    }
}
